/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package com.windea.study.kotlin.fxgl.sample.sandbox

import com.almasb.fxgl.app.*
import com.almasb.fxgl.core.math.*
import com.almasb.fxgl.dsl.*
import com.almasb.fxgl.dsl.components.*
import com.almasb.fxgl.entity.*
import javafx.application.*
import javafx.geometry.*
import javafx.scene.*
import javafx.scene.control.*
import javafx.scene.layout.*
import javafx.scene.paint.*
import javafx.scene.text.*
import javafx.stage.*
import javafx.util.*

class FXApp : Application() {
	override fun start(stage: Stage) {
		
		val stage1 = Stage()
		
		GameApplication.customLaunch(MyGame(), stage1)
		
		val root = BorderPane()
		root.setPrefSize(800.0, 600.0)
		root.top = HBox(Button("Button1"), Button("Button2"), Button("Button3"), Button("Button4"))
		root.bottom = Slider()
		
		val area = TextArea()
		area.text = "Some text in this area"
		area.font = Font.font(36.0)
		area.prefWidth = 190.0
		
		root.left = area
		root.right = Text("RIGHT")
		
		val pane = Pane()
		pane.setPrefSize(400.0, 400.0)
		
		root.center = pane
		
		
		stage1.x = stage.x + 800 / 2 - 400 / 2
		stage1.y = stage.y + 600 / 2 - 400 / 2
		stage1.isAlwaysOnTop = true
		
		stage.xProperty().addListener { observable, oldValue, newValue -> stage1.x = (newValue.toInt() + 800 / 2 - 400 / 2).toDouble() }
		
		stage.yProperty().addListener { observable, oldValue, newValue -> stage1.y = (newValue.toInt() + 600 / 2 - 400 / 2).toDouble() }
		
		stage.scene = Scene(root)
		stage.setOnCloseRequest { getGameController().exit() }
		stage.show()
	}
	
	object Launcher {
		@JvmStatic
		fun main(args: Array<String>) {
			Application.launch(FXApp::class.java, *args)
		}
	}
	
	inner class MyGame : GameApplication() {
		
		override fun initSettings(settings: GameSettings) {
			settings.width = 400
			settings.height = 400
			settings.title = "FXGL Embedded"
			settings.stageStyle = StageStyle.UNDECORATED
		}
		
		override fun initGameVars(vars: MutableMap<String, Any>) {
			vars["entities"] = 0
		}
		
		override fun initGame() {
			getGameWorld().addEntityFactory(MyFactory())
			getGameScene().setBackgroundColor(Color.BLACK)
			
			val text = getUIFactory().newText("", Color.WHITE, 24.0)
			text.textProperty().bind(getip("entities").asString("Entities: %d"))
			
			addUINode(text, 25.0, 25.0)
			
			run({ this.spawnCrystal() }, Duration.seconds(3.0))
		}
		
		private fun spawnCrystal() {
			val numToSpawn = 100
			
			for(i in 0 until numToSpawn) {
				spawn("crystal")
			}
			
			inc("entities", +numToSpawn)
		}
		
		inner class MyFactory : EntityFactory {
			
			@Preload(100)
			@Spawns("crystal")
			fun newEntity(data: SpawnData): Entity {
				return entityBuilder().at(FXGLMath.randomPoint(Rectangle2D(0.0, 0.0, (getAppWidth() - 55).toDouble(), (getAppHeight() - 55).toDouble())))
					.viewWithBBox(texture("ball.png", 32.0, 32.0))
					.with(RandomMoveComponent(Rectangle2D(0.0, 0.0, getAppWidth().toDouble(), getAppHeight().toDouble()), 250.0))
					.build()
			}
		}
	}
}
